<script setup>
import { useRouter } from "vue-router";
const router = useRouter();
import { ref } from "vue";
import { ElMessage } from "element-plus";
import { fetchRegister } from "@/api/user";
const ruleForm = ref({
  nickname: "",
  username: "",
  password: "",
  level: "0",
});

const rules = {
  nickname: [{ required: true, message: "昵称不能为空！", trigger: "blur" }],
  name: [{ required: true, message: "用户名不能为空！", trigger: "blur" }],
  password: [{ required: true, message: "密码不能为空！", trigger: "blur" }],
};

const submitForm = () => {
  fetchRegister(ruleForm.value).then((res) => {
    console.log(res.data.code);
    if (res.data.code == "200") {
      ElMessage.success("注册成功");
      router.push("/login");
    } else {
      ElMessage.error("注册失败");
    }
  });
};

const register = () => {};
</script>
<template>
  <div class="bj">
    <el-card class="box-card">
      <h2>注册</h2>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="formRef"
        label-position="left"
        label-width="70px"
        class="login-form"
      >
        <el-form-item label="昵称">
          <el-input v-model="ruleForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            clearable
            show-password
            @keyup.enter.native="submitForm('ruleForm')"
          />
        </el-form-item>
        <div style="margin-left: 20%">
          <el-radio-group v-model="ruleForm.level">
            <el-radio label="0">学生</el-radio>
            <el-radio label="1">教师</el-radio>
          </el-radio-group>
        </div>
      </el-form>
      <div class="flex" style="margin-top: 50px">
        <el-button
          type="primary"
          @click="submitForm()"
          v-loading="loading"
          class="denglu"
          >注册</el-button
        >
        <router-link to="/login">
          <el-button class="zhuche">去登录</el-button>
        </router-link>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.flex {
  display: flex;
  justify-content: space-between;
}
.denglu {
  margin-right: 20%;
  margin-left: 20%;
}
.bj {
  background-image: url("@/assets/init/LoginGround.jpg");
  background-size: cover;
  background-attachment: fixed;
  height: 100vh;
  display: flex;
  justify-content: right;
  align-items: center;
  padding-right: 10%;
}
.box-card {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 400px;
  height: 440px;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.7);
  box-shadow: 5px 5px 0 0 rgba(0, 0, 0, 0.2);
}
</style>
